//
//  ReviseNameViewController.m
//  dangdang
//
//  Created by air on 16/1/26.
//  Copyright © 2016年 luweijie. All rights reserved.
//

#import "ReviseNameViewController.h"

@interface ReviseNameViewController ()

@end

@implementation ReviseNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    
    [self.view addGestureRecognizer:singleTap];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
}

-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer

{
    [self.view endEditing:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _nameTextField.text = _PersonalProfileViewController.nameLabel.text;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    _PersonalProfileViewController.nameLabel.text = _nameTextField.text;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)save{
    if ([_nameTextField.text isEqual:nil] || [_nameTextField.text isEqualToString:@""]) {
        [self alertViewShowWithAlertString:@"姓名不能为空"];
        return;
    }
    
    NSString * nameString = _nameTextField.text;
    NSString * token = userObject.accessToken;
    NSString * uid = userObject.userID;
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:token forKey:@"token"];
    [params setObject:uid forKey:@"uid"];
    [params setObject:nameString forKey:@"uname"];
    [params setObject:@"0" forKey:@"udevice"];
    
    [self postRequestWithMethodName:@"user/modify" andParams:params andSender:nil];
    [LoadingView showInView:self.view image:nil info:@"通讯中，请稍候..." animated:YES modeled:YES autoHide:NO];
}

//网络连接返回数据
- (void)requestSuccessedWithResult:(id)result NSURLSessionTask:(NSURLSessionTask *)task sender:(id)sender{
    
    NSString * urlString = [NSString stringWithFormat:@"%@", task.currentRequest.URL];
    
    if ([result isKindOfClass:[NSDictionary class]]) {
                NSString * code = [NSString stringWithFormat:@"%@", [result objectForKey:@"retcode"]];
        if ([urlString isEqualToString:[NSString stringWithFormat:@"%@%@", URLDoman, @"user/modify"]] && [code isEqualToString:@"0"]){
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"个人资料" message:@"保存成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    userObject.personString = nil;
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }];
            [alertVC addAction:action];
            [self presentViewController:alertVC animated:YES completion:nil];
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
